package edu.codelibary.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JsonUtil {
	private static final String TAG = "JsonUtil";

	public static List<Map<String, Object>> string2Map(JSONArray array) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> item = null;
		JSONObject info = null;
		for (int i = 0; i < array.length(); i++) {
			item = new HashMap<String, Object>();
			try {
				info = array.getJSONObject(i);
				item = new HashMap<String, Object>();
				JSONArray names = info.names();
				for (int j = 0; j < names.length(); j++) {
					String name = names.getString(j);
					String value = info.getString(name);
					item.put(name, value);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			result.add(item);
		}
		Log.i(TAG, result.toString());
		return result;
	}
}
